package com.jz.community.moduleshopping.integralGoods.ui;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.commview.callback.EmptyCallback;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.R2;
import com.jz.community.moduleshopping.integralGoods.adapter.IntegralGoodsHistoryAdapter;
import com.jz.community.moduleshopping.integralGoods.bean.IntegralHistoryInfo;
import com.jz.community.moduleshopping.integralGoods.task.GetIntegralHistoryTask;
import com.jz.community.moduleshopping.integralGoods.ui.view.IntegralCouponFilterPop;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.kingja.loadsir.core.Transport;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Tony.Fan on 2018/7/3 17:49
 */
public class IntegralExchangeHistoryActivity extends BaseMvpActivity implements OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R2.id.title_back_left)
    ImageView titleBackLeft;
    @BindView(R2.id.title_name)
    TextView titleName;
    @BindView(R2.id.title_right)
    TextView titleRight;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;
    @BindView(R2.id.integral_recyclerView)
    RecyclerView integralRecyclerView;
    @BindView(R2.id.integral_goods_refresh)
    SmartRefreshLayout integralGoodsRefresh;
    @BindView(R2.id.title_right_iv)
    ImageView titleRightIv;
    @BindView(R2.id.integral_exchange_layout)
    LinearLayout integralExchangeLayout;
    private IntegralGoodsHistoryAdapter integralGoodsHistoryAdapter;
    private int page = 0;
    private IntegralHistoryInfo integralHistoryInfo;
    private IntegralCouponFilterPop integralCouponFilterPop;
    private int integralType = 0;
    private LoadService loadService;

    @Override
    protected int getContentView() {
        return R.layout.module_shopping_integral_goods_layout;
    }

    @Override
    protected void initView() {
        super.initView();
        setImmersionBar(titleToolbar);
        initTitle("兑换记录", "");
        titleRightIv.setImageResource(R.mipmap.integral_coupon_filter);
        SHelper.vis(titleRightIv);
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        loadService = LoadSir.getDefault().register(integralExchangeLayout);
        loadService.setCallBack(EmptyCallback.class, new Transport() {
            @Override
            public void order(Context context, View view) {
                ImageView empty_iv = view.findViewById(R.id.empty_img);
                TextView empty_tv = view.findViewById(R.id.empty_tv);
                empty_iv.setImageResource(R.mipmap.empty_default);
                empty_tv.setText("暂无兑换记录！");
            }
        });
        handleBindAdapter();
        getIntegralData(integralType, true);
    }

    @Override
    protected void addListener() {
        super.addListener();
        titleRightIv.setOnClickListener(v -> integralFilterPop());
    }

    private void getIntegralData(int type, boolean isRefresh) {
        new GetIntegralHistoryTask(this, o -> {
            integralHistoryInfo = (IntegralHistoryInfo) o;
            if (!Preconditions.isNullOrEmpty(integralHistoryInfo)) {
                setData(isRefresh, integralHistoryInfo.get_embedded(), integralHistoryInfo.getPage().getTotalPages());
                loadService.showSuccess();
                integralGoodsRefresh.finishRefresh();
            } else {
                loadService.showCallback(EmptyCallback.class);
            }
        }).execute(page + "", type + "");
    }

    private void integralFilterPop() {
        integralCouponFilterPop = new IntegralCouponFilterPop(this, titleRightIv);
        integralCouponFilterPop.handleData(integralType);
        integralCouponFilterPop.setIntegralHistoryListener(new IntegralCouponFilterPop.IntegralHistoryListener() {
            @Override
            public void integralResult(int type) {
                page = 0;
                integralType = type;
                getIntegralData(type, true);
            }
        });
    }

    private void handleBindAdapter() {
        LinearLayoutManager main_layoutManager = new LinearLayoutManager(this);
        integralRecyclerView.setLayoutManager(main_layoutManager);
        integralGoodsHistoryAdapter = new IntegralGoodsHistoryAdapter(new ArrayList<>());
        integralGoodsRefresh.setOnRefreshListener(this::onRefresh);
        integralGoodsHistoryAdapter.setOnLoadMoreListener(this, integralRecyclerView);
        integralGoodsRefresh.setEnableLoadMore(false);
        integralGoodsHistoryAdapter.setEnableLoadMore(false);
        integralRecyclerView.setAdapter(integralGoodsHistoryAdapter);
    }

    private void setData(boolean isRefresh, IntegralHistoryInfo.EmbeddedBean data, int totalPages) {

        page++;

        if (isRefresh) {
            integralGoodsHistoryAdapter.setNewData(data.getIntegraViews());
        } else {
            integralGoodsHistoryAdapter.addData(data.getIntegraViews());
        }

        if (totalPages <= page) {
            integralGoodsHistoryAdapter.loadMoreEnd();
        } else {
            integralGoodsHistoryAdapter.loadMoreComplete();
        }
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    @Override
    public void onLoadMoreRequested() {
        getIntegralData(integralType, false);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        page = 0;
        getIntegralData(integralType, true);
        integralGoodsHistoryAdapter.setEnableLoadMore(false);
    }
}
